package main

import (
	"global/libs/structs"
	"global/core"
	"github.com/ngaut/log"
	"os"
)

type Ticker struct {
	Slug 		string
	Symbols 		[]structs.Symbol
}

func NewTicker(slug string) *Ticker {
	return &Ticker{
		Slug:		slug,
		Symbols: 	make([]structs.Symbol, 0),
	}
}

func (t *Ticker) Run() {
	err := t.LoadSymbols()
	if err != nil {
		log.Errorf("Error: load symbols error, %s", err.Error())
		os.Exit(1)
	}
	log.Infof("symbols counts: %d", len(t.Symbols))
	go t.runDepth()
}

func (t *Ticker) runDepth() {
	ticker := NewTickerFactory(slug, t.Symbols)
	if ticker == nil {
		log.Errorf("Slug not supported.")
		os.Exit(1)
	}
	err := ticker.ListenAndAccept()
	if err != nil {
		log.Errorf("Error: listen and accept error, msg: %s", err.Error())
	}
}


// 获取交易所交易对
func (t *Ticker) LoadSymbols() error {
	conn := ctx.GetBaseRedis().Get()
	defer func() {
		if conn != nil {
			conn.Close()
		}
	}()

	var err error
	t.Symbols, err = core.GetSymbolsByExchange(conn, t.Slug)
	if err != nil {
		return err
	}

	return nil
}